import hx.core.Hxmaker;
import hx.assets.Assets;
import hx.ui.UIManager;
import haxe.io.Path;
import hx.layout.AnchorLayout;
import hx.layout.AnchorLayoutData;
import hx.ui.UIAssets;
import hx.display.Stage;
import hx.display.FPS;

/**
 * 编辑器
 */
class EditerMain extends Stage {
	override function onInit() {
		super.onInit();
		untyped window.perview = this.perview;
		untyped window.updateSize = this.updateSize;
		var fps = new FPS();
		this.topView.addChild(fps);
		this.topView.layout = new AnchorLayout();
		fps.layoutData = AnchorLayoutData.bottomLeft();
		trace("初始化编辑器");
	}

	public function updateSize(width:Int, height:Int):Void {
		Hxmaker.engine.init(width, height);
	}

	/**
	 * 预览界面
	 * @param xml 
	 */
	public function perview(projectPath:String, path:String, xmlContext:String):Void {
		this.removeChildren();
		trace("开始加载", path);
		UIManager.clean();
		var uiAssets = new UIAssets(path);
		uiAssets.strings.set(Assets.formatName(path), xmlContext);
		UIManager.bindAssets(uiAssets);
		uiAssets.nativePath = Path.directory(projectPath);
		uiAssets.onComplete(a -> {
			var view = uiAssets.build(this, true);
			trace("预览成功", view);
		}).onError(e -> {
			trace("预览失败", e);
		});
		uiAssets.start();
	}
}
